﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace Studio77.GuessTheWordsWin8.Utils
{
    public class JsonHelper
    {
        public static string ToJson<T>(T obj)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                ds.WriteObject(ms, obj);
                string strJSON = Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length);
                return strJSON;
            }
        }

        public static T Deserialize<T>(string sJson) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sJson)))
            {
                T obj = (T)ds.ReadObject(ms);
                return obj;
            }
        }
    }
}
